package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.Result;
import com.gxar.common.entity.SlippageResult;
import com.gxar.common.entity.SystemCodeEnum;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksDraftInfoResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateInfoResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateListResponse;
import com.gxar.quick.ar.server.db.service.WorksTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/8 15:41
 */
@Api(tags = "作品模板接口")
@RestController
@RequestMapping("/api/works/template")
@RequiredArgsConstructor
@Slf4j
public class WorksTemplateController {

    private final WorksTemplateService worksTemplateService;

    @ApiOperation("作品模板列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "next_id", value = "下一个查询的id", dataType = "string"),
            @ApiImplicitParam(name = "product_category", value = "产品类别", dataType = "string"),
    })
    @GetMapping("")
    public Result<SlippageResult<WorksTemplateListResponse>> getList(@RequestParam(name = "next_id", required = false) String nextId,
                                                                     @RequestParam(name = "product_category", required = false, defaultValue = "0") String productCategory) {
        UserProfile userProfile = LoginUserThread.get();
        SlippageResult<WorksTemplateListResponse> worksTemplateListResponse = worksTemplateService.templateList(userProfile, nextId, productCategory);
        return Result.success(worksTemplateListResponse);
    }

    @ApiOperation("查看模板详情")
    @ApiImplicitParam(name = "work_template_id", value = "作品模板id", dataType = "long", required = true, paramType = "path")
    @GetMapping("/{work_template_id}")
    public Result<WorksTemplateInfoResponse> getTemplateDetail(@PathVariable(name = "work_template_id") String id) {
        UserProfile userProfile = LoginUserThread.get();
        if (StringUtils.isBlank(id) || "null".equals(id)) {
            return Result.error(SystemCodeEnum.PARAM_ERROR);
        }
        return worksTemplateService.getTemplateDetail(userProfile, Long.parseLong(id));
    }
}
